package com.zxl.yjb.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.position.PositionSelectActivity;
import com.zxl.yjb.adapter.PositionAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.AddressBookBean;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.bean.PoiItemExt;
import com.zxl.yjb.bean.PoiItemSB;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.SaveBeanUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PositionBookActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.rv_position_list)
    RecyclerView rvPositionList;

    private DataManager dataManager;

    private ArrayList<PoiItem> poiItemList = new ArrayList<>();
    private ArrayList<AddressBookBean.DataBean> dataBeans = new ArrayList<>();
    private PositionAdapter positionAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_position_book);
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyPosition();
    }

    @Override
    protected void initView() {
        tvPageTitle.setText("地址簿");
        tvConfirm.setVisibility(View.VISIBLE);
        tvConfirm.setText("添加");

        layoutManager = new LinearLayoutManager(this);
        rvPositionList.setLayoutManager(layoutManager);
        positionAdapter = new PositionAdapter(R.layout.item_position_search, poiItemList);
        rvPositionList.setAdapter(positionAdapter);
        positionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(PositionBookActivity.this,PositionAddActivity.class);
                intent.putExtra("id",poiItemList.get(position).getPoiId());
                intent.putExtra("dataBean",dataBeans.get(position));
                startActivity(intent);
            }
        });

        positionAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                DisplayUtil.diaplayDialog(PositionBookActivity.this, "提示", "确定要删除此条记录吗？", "取消", "确定", null, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeHisPosition(position);
                    }
                });

                return true;
            }
        });
    }

    private void removeHisPosition(int position) {

        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("id",poiItemList.get(position).getPoiId());
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {

            }
        };
        dataManager.addressBook_del(new ProgressObserver<Object>(listener,this),map);


        if (poiItemList == null)
            return;
        if (position > poiItemList.size() - 1)
            return;
        poiItemList.remove(position);
        positionAdapter.notifyDataSetChanged();


    }

    private void getMyPosition() {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("page", 0 + "");
        map.put("count", 100 + "");
        ObserverOnNextListener<AddressBookBean> listener = new ObserverOnNextListener<AddressBookBean>() {
            @Override
            public void onNext(AddressBookBean addressBookBean) {
                dataBeans.clear();
                poiItemList.clear();
                dataBeans.addAll(addressBookBean.getData());
                for (AddressBookBean.DataBean poiItemSB : addressBookBean.getData()) {
                    PoiItem poiItem = new PoiItem(poiItemSB.getId()+"", new LatLonPoint(poiItemSB.getLatitude(), poiItemSB.getLongitude()), poiItemSB.getName(), poiItemSB.getCity() + poiItemSB.getDistrict() + poiItemSB.getStreet() + poiItemSB.getOthers());
                    poiItemList.add(poiItem);
                }
                positionAdapter.notifyDataSetChanged();
            }
        };
        dataManager.addressBook_list(new ProgressObserver<AddressBookBean>(listener, this), map);

    }

    @OnClick({R.id.iv_back, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_confirm:
                Intent intent = new Intent(PositionBookActivity.this, PositionAddActivity.class);
                startActivity(intent);
                break;
        }
    }
}
